/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pmi.dao.implement;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import pmi.dao.interf.pasieninterface;
import pmi.dao.koneksi.databaseutilities;
import pmi.dao.model.pasien;

/**
 *
 * @author VPC-CW25FG
 */
public class pasienimplement implements pasieninterface{

    public void insert(pasien pasien1) {
        try {
            PreparedStatement ps = databaseutilities.getConnection().prepareStatement("insert into pasien values(?,?,?,?,?,?,?,?,?,?,?)");
            ps.setString(1, pasien1.getId());
            ps.setString(2, pasien1.getNama());
            ps.setString(3, pasien1.getAlamat());
            ps.setString(4, pasien1.getTelp());
            ps.setString(5, pasien1.getTanggal());
            ps.setString(6, pasien1.getUmur());
            ps.setString(7, pasien1.getGoldar());
            ps.setString(8, pasien1.getKelamin());
            ps.setString(9, pasien1.getBerat());
            ps.setString(10, pasien1.getTinggi());
            ps.setString(11, pasien1.getKeterangan());
            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(pasienimplement.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(String id, pasien pasien1) {
        try {
            PreparedStatement ps = databaseutilities.getConnection().prepareStatement("update pasien set nama=?,alamat=?,telp=?,tanggal=?,umur=?,gol=?,LP=?,berat=?,tinggi=?,keterangan=? where id=?");
            ps.setString(1, pasien1.getNama());
            ps.setString(2, pasien1.getAlamat());
            ps.setString(3, pasien1.getTelp());
            ps.setString(4, pasien1.getTanggal());
            ps.setString(5, pasien1.getUmur());
            ps.setString(6, pasien1.getGoldar());
            ps.setString(7, pasien1.getKelamin());
            ps.setString(8, pasien1.getBerat());
            ps.setString(9, pasien1.getTinggi());
            ps.setString(10, pasien1.getKeterangan());
            ps.setString(11, pasien1.getId());
            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(pasienimplement.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    @Override
    public void delete(String id) {
        try {
            PreparedStatement ps = databaseutilities.getConnection().prepareStatement("delete from pasien where id=?");
            ps.setString(1, id);
            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(pasienimplement.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    @Override
    public List<pasien> getAll() {
        try {
            Statement st = databaseutilities.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from pasien");
            List<pasien> list = new ArrayList<pasien>();
            while (rs.next()) {
                pasien p = new pasien();
                p.setId(rs.getString("id"));
                p.setNama(rs.getString("nama"));
                p.setAlamat(rs.getString("alamat"));
                p.setTelp(rs.getString("telp"));
                p.setTanggal(rs.getString("tanggal"));
                p.setUmur(rs.getString("umur"));
                p.setGoldar(rs.getString("gol"));
                p.setKelamin(rs.getString("LP"));
                p.setBerat(rs.getString("berat"));
                p.setTinggi(rs.getString("tinggi"));
                p.setKeterangan(rs.getString("keterangan"));
                list.add(p);
            }
            return list;
        } catch (SQLException ex) {
            Logger.getLogger(pasienimplement.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        
    }

    @Override
    public pasien getById(String id) {
        try {
            PreparedStatement ps=databaseutilities.getConnection().prepareStatement("select *from pasien where id=?");
            ps.setString(1, id);
            ResultSet rs=ps.executeQuery();
            pasien p=new pasien();
            if(rs.next()){
                p.setId(rs.getString("ID"));
                p.setNama(rs.getString("nama"));
                p.setAlamat(rs.getString("alamat"));
                p.setTelp(rs.getString("telp"));
                p.setUmur(rs.getString("umur"));
                p.setTanggal(rs.getString("tanggal"));
                p.setBerat(rs.getString("berat"));
                p.setTinggi(rs.getString("tinggi"));
                p.setKeterangan(rs.getString("keterangan"));
                p.setKelamin(rs.getString("LP"));
            }
            return p;
        } catch (SQLException ex) {
            Logger.getLogger(pasienimplement.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
